package com.weight.system.fix.web;

import com.weight.system.fix.PO.FixInfo;
import com.weight.system.fix.VO.FixInfoVO;
import com.weight.system.fix.service.FixInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 设备维修记录表
 */
@RestController
@Slf4j
@RequestMapping("/fix/fixInfo")
public class FixInfoController {

	@Autowired
	private FixInfoService fixInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询设备维修记录表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForFixInfoList",method=RequestMethod.GET)
	public ModelAndView queryForFixInfoList(){
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/fix/queryForFixInfoList");
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 2.查询设备维修记录表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForFixInfoListDG",method=RequestMethod.GET)
	public String queryForFixInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String equmtId = request.getParameter("equmtId");
			String fixDate = request.getParameter("fixDate");
			String fixDesc = request.getParameter("fixDesc");
			String cospicPath = request.getParameter("cospicPath");
			String fixPern = request.getParameter("fixPern");

			Map<Object, Object> map =new HashMap<>();
			map.put("equmtId", equmtId);
			map.put("fixDate", DateFormUtils.getStringToDate(fixDate, pattern));
			map.put("fixDesc", fixDesc);
			map.put("cospicPath", cospicPath);
			map.put("fixPern", fixPern);

			
			//2、查询设备维修记录表列表
			List<FixInfo> fixInfoList = fixInfoService.findFixInfoByPage(page,rows,map);
			log.info("fixInfoList={}",fixInfoList);
			List<FixInfoVO> fixInfoVOList = fixInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<FixInfoVO> result = new PageResult<FixInfoVO>(fixInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param fixInfoVO
	 * @param fixInfo
	 * @return
	 */
	public void changeVOToPO(FixInfoVO fixInfoVO,FixInfo fixInfo){
			BeanUtils.copyProperties(fixInfoVO,fixInfo);
			fixInfo.setFixDate(DateFormUtils.getStringToDate(fixInfoVO.getFixDate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param fixInfoVO
	 * @param fixInfo
	 * @return
	 */
	public void changePoToVO(FixInfoVO fixInfoVO,FixInfo fixInfo){
			BeanUtils.copyProperties(fixInfo,fixInfoVO);
			fixInfoVO.setFixDate(DateFormUtils.getDateToString(fixInfo.getFixDate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param fixInfo
	 * @return
	 */
	public FixInfoVO changePoToVO(FixInfo fixInfo){
			FixInfoVO fixInfoVO=new FixInfoVO();
			BeanUtils.copyProperties(fixInfo,fixInfoVO);
			fixInfoVO.setFixDate(DateFormUtils.getDateToString(fixInfo.getFixDate(), pattern));
			

			return fixInfoVO;
	}

	/**
	 * 3.查询设备维修记录表--详情页
	 * @param fixInfoId
	 * @return
	 */
	@RequestMapping("/queryForFixInfoDetail")
	public ModelAndView queryForFixInfoDetail(@RequestParam(value="fixInfoId",required=false) String fixInfoId){
			//1.校验id
			CommonUtils.validateExistID(fixInfoId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("fixInfoId", fixInfoId);
			log.info("查询设备维修记录表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			FixInfo fixInfo = fixInfoService.findByFixInfoId(map);
			
			FixInfoVO fixInfoVO = changePoToVO(fixInfo);
			//3、返回前台
			log.info("查询设备维修记录表查询成功：fixInfo={}",fixInfo);
			modelAndView.setViewName("/system/fix/queryForFixInfoDetail");
			modelAndView.addObject("fixInfoVO",fixInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改设备维修记录表操作
	 * @param fixInfoId
	 * @return
	 */
	@RequestMapping(value="/addFixInfo")
	public ModelAndView addFixInfo(@RequestParam(value="fixInfoId",required=false) String fixInfoId){
			//1、创建返回对象
			FixInfoVO fixInfoVO = new FixInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(fixInfoId)) {
				Map<String, String> map=new HashMap<>();
				map.put("fixInfoId", fixInfoId);
				log.info("修改操作，查询ID：fixInfoId={}",fixInfoId);
				FixInfo fixInfo = fixInfoService.findByFixInfoId(map);
				this.changePoToVO(fixInfoVO,fixInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/fix/addFixInfo");
			modelAndView.addObject("fixInfoVO",fixInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改设备维修记录表操作
	 * @param fixInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveFixInfo")
	public JsonResultUtil saveFixInfo(@Valid FixInfoVO fixInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,fixInfoVO);
			
			//2.获取id
			String fixInfoId=fixInfoVO.getFixInfoId();
			FixInfo fixInfo=new FixInfo();
			BeanUtils.copyProperties(fixInfoVO,fixInfo);
			this.changeVOToPO(fixInfoVO,fixInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(fixInfoId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("fixInfoId", fixInfoId);
				log.info("修改操作，查询ID：fixInfoId={}",fixInfoId);
				//查询是否存在
				fixInfoService.findByFixInfoId(map);
				try{
					fixInfo.setUpdateBy("000");
					fixInfoService.updateFixInfo(fixInfo);
					log.info("更新操作成功：fixInfo={}",fixInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					fixInfo.setFixInfoId(CommonUtils.getUUId());
					fixInfo.setCreateBy("000");
					fixInfo.setUpdateBy("000");
					fixInfoService.insertFixInfo(fixInfo);
					log.info("添加操作成功：fixInfo={}",fixInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除设备维修记录表操作
	 * @param fixInfoId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByFixInfoId")
	public JsonResultUtil deleteByFixInfoId(@RequestParam(value="fixInfoId",required=false) String fixInfoId){
			//1.校验id
			CommonUtils.validateExistID(fixInfoId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("fixInfoId", fixInfoId);
			fixInfoService.findByFixInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除设备维修记录表入参，map={}",map);
			
			try{
				fixInfoService.upDelByFixInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除设备维修记录表操作---批量删除
	 * @param fixInfoIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByFixInfoIdBatch")
	public JsonResultUtil deleteByFixInfoIdBatch(@RequestParam(value="fixInfoIdList",required=false) List fixInfoIdList){
			//1.校验id
			CommonUtils.validateExistIDList(fixInfoIdList);
			
			//2.校验数据库
			fixInfoService.findByFixInfoIdList(fixInfoIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("fixInfoIdList", fixInfoIdList);
			map.put("updateBy","000");
			log.info("批量删除设备维修记录表操作入参，map={}",map);
			
			try{
				fixInfoService.upDelByFixInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}